import java.math.BigInteger;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

// 661 
public class P66 {
	public static void main(String[] args) {
		int maxD = 0;
		BigInteger max = BigInteger.ZERO;
		for (int i = 2; i <= 1000; i++) {

			if (Util.isSqure(i) > 0) {
				continue;
			}

			Num num = new P66.Num();
			num.n = i;
			num.numerator[0] = 1;
			num.numerator[1] = -1 * (int) Math.sqrt(num.n);
			num.denominator[0] = 0;
			num.denominator[1] = 1;
			num.a = -1 * num.numerator[1];

			int a = num.a;

			Set<Num> numSet = new LinkedHashSet<Num>();

			
			
			while (true) {
				num = reverse(num);
				num = cal(num);

				if (numSet.contains(num)) {
					System.out.print("root(" + i + ") = [" + a + ":(");
					Num[] nums = numSet.toArray(new P66.Num[0]);
					for (int n = 0; n < nums.length; n++) {
						System.out.print(nums[n].a);

						if (n != nums.length - 1) {
							System.out.print(",");
						}
					}
					System.out.println(")]");
					
					int c = 0;
					while (true) {
						BigInteger[] x = getFraction(nums, a, c);
						//System.out.println(Arrays.toString(x));
						c++;
						
						if (x[0].pow(2).subtract(x[1].pow(2).multiply(new BigInteger(String.valueOf(i)))).compareTo(BigInteger.ONE) == 0){
							System.out.println(Arrays.toString(x));
							if (max.compareTo(x[0]) < 0) {
								max = x[0];
								maxD = i;
							}							

							break;
						}
					}
					
					
					break;
				}
				numSet.add(num);
			}

			// System.out.println(i);
		}

		System.out.println(">>>" + max);
		System.out.println(">>>" + maxD);
	}

	public static BigInteger[] getFraction(Num[] nums, int a, int y) {
		BigInteger[] x = new BigInteger[2];

		for (int i = y; i >= 0; i--) {
			int index = i % nums.length;
			Num n = nums[index];
			Num prevN = null;
			if (index == 0) {
				prevN = nums[nums.length - 1];
			} else {
				prevN = nums[index - 1];
			}

			//System.out.println(">" + n.a);

			if (i == y) {
				x[0] = new BigInteger(String.valueOf(n.a));
				x[1] = BigInteger.ONE;
			} else {

				x[0] = x[1].multiply(new BigInteger(String.valueOf(n.a)))
						.add(x[0]);

			}
			BigInteger temp = x[0];
			x[0] = x[1];
			x[1] = temp;
			//System.out.println(Arrays.toString(x));
		}
		//System.out.println(">>>" + nums[0].a);
		x[0] = x[1].multiply(new BigInteger(String.valueOf(a))).add(x[0]);
		return x;
	}

	public static Num cal(Num num) {
		Num newNum = new Num();
		newNum.n = num.n;
		newNum.denominator[0] = 0;
		newNum.denominator[1] = num.n - num.denominator[1] * num.denominator[1];
		newNum.denominator[1] /= num.numerator[1];

		newNum.numerator[0] = 1;
		newNum.numerator[1] = -1 * num.denominator[1];

		int max = (int) Math.sqrt(newNum.n);
		// System.out.println(max);
		while (newNum.numerator[1] - newNum.denominator[1] >= -1 * max) {
			newNum.numerator[1] -= newNum.denominator[1];
			newNum.a++;
		}

		return newNum;
	}

	public static Num reverse(Num num) {
		Num newNum = new Num();
		newNum.n = num.n;
		newNum.numerator = num.denominator;
		newNum.denominator = num.numerator;
		return newNum;
	}

	public static String notation(int[] x, int n) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("( ");
		if (x[0] != 0) {
			buffer.append("root(" + n + ") ");
		}

		if (x[1] > 0) {
			if (x[0] != 0) {
				buffer.append(" + ");
			}
			buffer.append(x[1]);
		} else if (x[1] < 0) {
			if (buffer.length() > 0) {
				buffer.append(" - ");
			} else {
				buffer.append("-");
			}
			buffer.append(-1 * x[1]);
		}
		buffer.append(" )");

		return buffer.toString();
	}

	public static void print(Num num) {
		String n = notation(num.numerator, num.n);
		String d = notation(num.denominator, num.n);

		if (num.a != 0) {
			System.out.print(num.a + " + ");
		}

		System.out.print(n);

		System.out.print("/");
		System.out.print(d);
		System.out.println();
	}

	public static class Num {
		int[] numerator = new int[2];
		int[] denominator = new int[2];
		int a = 0;
		int n = 0;

		@Override
		public boolean equals(Object obj) {
			Num x = (Num) obj;
			return n == x.n && numerator[0] == x.numerator[0]
					&& numerator[1] == x.numerator[1]
					&& denominator[0] == x.denominator[0]
					&& denominator[1] == x.denominator[1] && a == x.a;
		}

		@Override
		public int hashCode() {
			StringBuffer buffer = new StringBuffer();
			buffer.append(n);
			buffer.append(",");
			buffer.append(a);
			buffer.append(",");
			buffer.append(numerator[0]);
			buffer.append(",");
			buffer.append(numerator[1]);
			buffer.append(",");
			buffer.append(denominator[0]);
			buffer.append(",");
			buffer.append(denominator[1]);
			return buffer.toString().hashCode();
		}
	}
}
